package cn.com.edaily.auth.help;

import cn.com.edaily.auth.utils.RsaKeyFactory;
import cn.com.edaily.auth.utils.RsaUtil;
import cn.com.edaily.common.exception.BusinessException;
import org.springframework.stereotype.Component;

/**
 * <pre>类名: AuthHelper</pre>
 * <pre>描述: 认证帮助类</pre>
 * <pre>版权: web_chen@163.com</pre>
 * <pre>日期: 2020/1/31 14:17</pre>
 * <pre>作者: chenwb</pre>
 */
@Component
public class AuthHelper {

	/**
	 * @param passwordNeedDecrypt 需要解密的密码
	 * @param keyIndex            私钥索引
	 * @return String 解密后的密码
	 * @Description: 解密
	 * @author chenwb
	 * @date 2020/1/31 17:02
	 */
	public String decrypt(String passwordNeedDecrypt, Integer keyIndex) {
		String privateKey = RsaKeyFactory.getKeyList().get(keyIndex).getPrivateKey();
		try {
			return RsaUtil.decrypt(passwordNeedDecrypt, privateKey);
		} catch (Exception e) {
			throw new BusinessException("解密失败:" + e.getMessage(), e.getCause());
		}
	}

}
